package net.wangds.config.ctrl;

import io.swagger.annotations.Api;
import net.wangds.config.comp.SysConfComp;
import net.wangds.config.module.Config;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.convertor.Page2MultipleRespConv;
import net.wangds.jpa.convertor.PagingForm2PageRequestConv;
import net.wangds.jpa.ctrl.AbstractJpaCompCtrl;
import net.wangds.web.entrypoint.BaseMultipleResp;
import net.wangds.web.entrypoint.BaseResp;
import net.wangds.web.entrypoint.PagingForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags="配置")
@RestController
@RequestMapping("/conf")
public class ConfigCtrl extends AbstractJpaCompCtrl<Config> {

    @Autowired
    private SysConfComp mmCmpConf;

    private PagingForm2PageRequestConv mmCovPg2Pr = new PagingForm2PageRequestConv(()->Sorts.createTm_desc);

    @Autowired
    private Page2MultipleRespConv<Config> mmCovPg2Mr;

    @Override
    protected AbstractRemovedComp<Config> fetchJpaComp() {
        return mmCmpConf;
    }

    @Override
    protected BaseMultipleResp<Config> listWithKeywords(PagingForm page) {
        return mmConvPage2Resp.convert(mmCmpConf.findByKeywords(page.getKeywords(), mmConvPf2Pr.convert(page)));
    }

    /**
     * 按照分类分页查询.
     * @param cata 分类.
     * @param form 分页信息.
     * @return 结果.
     */
    @RequestMapping(value="/byCata/{cata}", method = RequestMethod.POST)
    public BaseMultipleResp<Config> listByCata(
            @PathVariable("cata") String cata
            ,@RequestBody PagingForm form){
        return mmCovPg2Mr.convert(mmCmpConf.findByCata(cata, form.getKeywords(), mmCovPg2Pr.convert(form)));
    }

    /**
     * 按照分类和配置项代码查询.
     * @param cata 分类.
     * @param item 配置项代码.
     * @return 结果.
     */
    @RequestMapping(value="/byCataAndItem/{cata}/{item}", method = RequestMethod.POST)
    public BaseResp<Config> listByCataAndItem(
            @PathVariable("cata") String cata
            ,@PathVariable("item") String item){

        return BaseResp.of(mmCmpConf.findByCataAndItem(cata, item).orElse(null));
    }

    /**
     * 按照分类分页查询分类下所有配置.
     * @param cata 分类.
     * @return 结果.
     */
    @RequestMapping(value="/all/{cata}", method = RequestMethod.POST)
    public BaseMultipleResp<Config> listAllByCata(
            @PathVariable("cata") String cata){
        return BaseMultipleResp.list(mmCmpConf.findByCata(cata, Sorts.createTm_asc));
    }


    /**
     * 按照分类分页查询.
     * @param cata 分类.
     * @return 结果.
     */
    @RequestMapping(value="/tree/{cata}", method = RequestMethod.POST)
    public BaseMultipleResp<Config> tree(@PathVariable("cata") String cata){
        return BaseMultipleResp.list(mmCmpConf.configTree(cata));
    }






}
